libname a "D:\SAS\Dataset\CRSP\Monthly Return";

data a (keep=date cusip ticker prc PRIMEXCH ret SHROUT VWRETD);
set a.Return_1952_1979 a.Return_1980_2020 a.Return_2021_2022;
run;


libname b  "D:\SAS\Dataset\Fama French Factors";

data b (keep=dateff rf);
set b.ff5factormonthly;

data b (rename=(dateff=date));
set b;
run;

proc sort data=a; by date; 
proc sort data=b; by date;


data c; merge a b; by date;
run;

data c;
set c;
year=year(date);
month=month (date);
me=prc *shrout;
stockreturn=ret-rf;
MKTRF=VWRETD-rf;
run;


libname d "D:\SAS\Project\Persistence of MTB Ratio";

data d1 (keep=cusip fyear sgaintangible);
set d.sgaintangible;
run;

data d1 (rename=(fyear=year));
set d1;
run;


libname e  "D:\SAS\Dataset\Compustat\Compustat US";

data e1 (keep=DATADATE cusip at XRD sale COGS REVT INTPN XSGA ceq ni);
set e.Compustat1950_1979 e.Compustat1980_1989 e.Compustat1990_1995 e.Compustat1996_2000 e.Compustat2001_2005 
e.Compustat2006_2010  e.Compustat2011_2020 e.compustat2021_2023;
run;


data e1;
set e1;
if XRD=. then xrd=0;
year=year(DATADATE);
run;

proc sort data=d1; by cusip year;
proc sort data=e1; by cusip year;


data d2;
merge d1 e1; by cusip year;
run;

proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lat=lag(at);
if first.cusip then lat=.;
if sgaintangible<0 then sgaintangible=0;
run;

data d2;
set d2;
intangible=(sgaintangible+xrd)/((lat+at)/2);
assetgrowth=(at-lat)/lat;
profitability=(REVT-COGS-INTPN -XSGA)/ceq;
roe=ni/ceq;
run;

data d2;
set d2;
where intangible>=0;
run;


data c1 (keep=cusip year me);
set c; where month=12; 
run;

data d2;
set d2;
cusip=substr (cusip, 1, 8);
run;

proc sort data=d2; by cusip year;
proc sort data=c1; by cusip year;

data d2;
merge d2 c1; by cusip year;
mtb=me/ceq/1000;
run;


proc sort data=d2; by cusip year;

data d2;
set d2; by cusip year;
lintangible=lag(intangible);
lme=lag(me);
lmtb=lag(mtb);
lassetgrowth=lag(assetgrowth);
lprofitability=lag(profitability);
if first.cusip then lintangible =.;
if first.cusip then lme=.;
if first.cusip then lmtb=.;
if first.cusip then lassetgrowth=.;
if first.cusip then lprofitability=.;
run;


proc sort data=d2; by year lintangible;

proc rank data=d2 out=d3 group=4; by year; where lintangible>0; /* divide firms into 4 groups for those with non-zero value lintangible.*/
var lintangible;
ranks elintangible; 
run;


data d2;
set d2; where lintangible=0;
elintangible=-1; /* firms with zero-value lintangible are classified as one group*/
run;


data d4;
set d2 d3;
run;

proc sort data=d4; by elintangible; run; 


data d5 (keep=cusip year elintangible);
set d4;
run;


proc sort data=c; by cusip year;
proc sort data=d5; by cusip year;


data f;
merge c d5; by cusip year;
run;



libname g "D:\SAS\Dataset\Fama French Factors";

data g1 (keep=DATEFF smb hml rmw cma umd);
set g.ff5factormonthly;

data g1 (rename=(dateff=date));
set g1;
run;


data g2;
set g.sgafactor;
run;


proc sort data=f; by date;
proc sort data=g1; by date;
proc sort data=g2; by date;


data h;
merge f g1 g2; by date;
run;


proc sort data=h; by date elintangible; run;

proc means data=h ;by date elintangible;
output out= h1;
var stockreturn;
run;

data h1;
set h1;
where _STAT_= "MEAN";
run;

data h2 (keep=date year MKTRF smb hml RMW CMA UMD SGAfactor);
set h;
run;

proc sort data=h2 noduplicate; by date; run; 


proc sort data=h1; by date;
proc sort data=h2; by date;

data h3;
merge h1 h2; by date;
run;



/*Results for Table 6 Panel A*/


data h4;
set h3;
SGAfactorResidual=SGAfactor-(0.07817*MKTRF+0.19130*SMB-0.32947*HML+0.08906*RMW+0.13287*CMA); /* These coefficients are got when performing analysis on Table 5*/
RUN;

proc sort data=h4; by elintangible;

proc reg data=h4;  where 1962<year <=1992 and elintangible^=.; by elintangible;
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactorresidual;
run;



data h5;
set h3;
SGAfactorResidual=SGAfactor-(0.11794*MKTRF+0.22084*SMB-0.64662*HML-0.63268*RMW-0.00400*CMA);/* These coefficients are got when performing analysis on Table 5*/
RUN;

proc sort data=h5; by elintangible;

proc reg data=h5;  where 1992<year <=2022 and elintangible^=.;  by elintangible;
model stockreturn=MKTRF smb hml RMW CMA UMD SGAfactorresidual;
run;





